package com.itheima.health.config;

import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class CustomerUserDetail implements UserDetailsService {
    private static Map<String, User> userDb = new HashMap<>();

    /**
     * @Author: XuZhen
     * @Date: 下午 12:03 2021/3/8 0008
     * @Parms
     * @ReturnType:
     * @Description: 初始化用户角色信息
     */
    static {
        //初始化权限
        Permission addPer = new Permission("添加权限", "add");
        Permission findPer = new Permission("查询权限", "find");
        Permission updatePer = new Permission("更新权限", "update");
        Permission delPer = new Permission("删除权限", "del");


        //初始化角色
        Role adminRole = new Role("超级管理员", "ROLE_ADMIN");
        adminRole.getPermissions().add(addPer);
        adminRole.getPermissions().add(findPer);
        adminRole.getPermissions().add(updatePer);
        adminRole.getPermissions().add(delPer);


        Role role1 = new Role("角色1", "ROLE_01");
        role1.getPermissions().add(addPer);
        adminRole.getPermissions().add(findPer);

        Role role2 = new Role("角色2", "ROLE_02");
        role2.getPermissions().add(findPer);


        //初始化用户
        //密码加密
         User adminUser = new User();
        adminUser.setUsername("admin");
        adminUser.setPassword("{bcrypt}$2a$10$3xW2nBjwBM3rx1LoYprVsemNri5bvxeOd/QfmO7UDFQhW2HRHLi.C");
        adminUser.getRoles().add(adminRole);
        userDb.put(adminUser.getUsername(), adminUser);


        User zhangsan = new User();
        zhangsan.setUsername("zhangsan");
        zhangsan.setPassword("{bcrypt}$2a$10$3xW2nBjwBM3rx1LoYprVsemNri5bvxeOd/QfmO7UDFQhW2HRHLi.C");
        zhangsan.getRoles().add(role1);
        userDb.put(zhangsan.getUsername(), zhangsan);


        User lisi = new User();
        lisi.setUsername("lisi");
        lisi.setPassword("{bcrypt}$2a$10$3xW2nBjwBM3rx1LoYprVsemNri5bvxeOd/QfmO7UDFQhW2HRHLi.C");
        lisi.getRoles().add(role2);
        userDb.put(lisi.getUsername(), lisi);
    }

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user = userDb.get(userName);
        if (user == null) {
            return null;
        }


        StringBuffer authStr = new StringBuffer();//ROLE_01,add,   ROLE_02,find,
        Set<Role> roles = user.getRoles();
        for (Role role : roles) {
            //获取角色
            authStr.append(role.getKeyword());//ROLE_ADMIN  ROLE_01  ROLE_02
            authStr.append(",");
            //获取权限
            Set<Permission> permissions = role.getPermissions();
            for (Permission permission : permissions) {
                authStr.append(permission.getKeyword());
                authStr.append(",");
            }
        }
        //去掉字符串末尾,
        if (authStr.length() > 0) {
            authStr.deleteCharAt(authStr.length() - 1);
        }

        //创建security中的user返回
        return new org.springframework.security.core.userdetails.User(userName, user.getPassword(),
                AuthorityUtils.commaSeparatedStringToAuthorityList(authStr.toString()));
    }
}
